#!/usr/bin/env python3

from cpi import CPI
from os.path import dirname, abspath, join
import req as requests

CNY_RATE = float(requests.get("http://api.fixer.io/latest?base=USD").json()['rates']['CNY'])

def main():
    li = []
    with open(join(dirname(abspath(__file__)), "the_numbers_li.txt")) as f:
        for i  in f:
            year, total, url, name = i.strip().split(" ", 3)
            year = int(year)
            if year not in CPI:
                continue
            li.append(
                [
                    year,
                    CNY_RATE*CPI[year]*int(total)/100000000.0,
                    url,
                    name
                ]
            )
    li.sort(key=lambda x:-x[1])
    for pos, i in enumerate(li,1):
        i.insert(0, pos)
        print("%s %s %.4f %s %s"%tuple(i))



if __name__ == "__main__":
    main()
